﻿#region Using Statements
using System.Collections.Generic;
using System.Linq;
using DirectZZT.Shared.Core;
using DirectZZT.Shared.World;
using DirectZZT.Shared.World.Objects;
#endregion

namespace DirectZZT.BoardObjects
{
    /// <summary>
    /// Represents the boulder ("Boulder") board object.
    /// </summary>
    [Export("Boulder")]
    [BoardObject(Identifier = "Boulder", Classification = BoardObjectClassification.Terrain, AllowBecome = true)]
    public class BoulderBoardObject : BoardObject
    {
        #region Properties

        /// <summary>
        /// A boulder can be moved and has collision.
        /// </summary>
        public override BoardObjectCapabilities Capabilities
        {
            get { return base.Capabilities | BoardObjectCapabilities.Movable; }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the BoulderBoardObject class.
        /// </summary>
        public BoulderBoardObject()
        {
            this.Texture = "Textures/PlayfieldObjects/Boulder";
            this.Enabled = true;
            this.Visible = true;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Only DirectZZT supported.
        /// </summary>
        /// <param name="featureSet"></param>
        /// <returns></returns>
        protected override bool CanAttachScript(FeatureSet featureSet)
        {
            return featureSet == FeatureSet.DirectZZT;
        }

        /// <summary>
        /// Called when this object spawns.
        /// </summary>
        /// <param name="modifiers"></param>
        public override void OnSpawn(string[] modifiers)
        {
            base.OnSpawn(modifiers);
        }

        /// <summary>
        /// Called when this object is involved in a collision.
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public override CollisionResolveArgs OnCollide(CollisionArgs args)
        {
            if (!args.IsSource)
            {
                // precondition: can move?
                bool precondition = true;
                {
                    // start at our very own neighbor and iterate through them
                    BoardObject bo = this.GetNeighbor(args.CollisionSide);
                    while (bo != null)
                    {
                        // we don't move when the object is not movable
                        if (!bo.Capabilities.HasFlag(BoardObjectCapabilities.Movable))
                        {
                            precondition = false;
                            break;
                        }

                        bo = bo.GetNeighbor(args.CollisionSide);
                    }
                }

                // move everything in the given direction
                if (precondition)
                {
                    int oldX = this.X;
                    int oldY = this.Y;

                    // for each object in the list, go from farthest to nearest
                    foreach (BoardObject objToMove in GetObjectsToMove(args.CollisionSide).Reverse().ToList())
                    {
                        // apply delta, thus moving the object (if possible)
                        objToMove.TryMoveTo(objToMove.X += args.CollisionSide.GetDeltaX(), objToMove.Y += args.CollisionSide.GetDeltaY());
                    }

                    // finally, move source object to new "old" place of boulder
                    if (this.Board.GetObjectAt(oldX, oldY) == null)
                    {
                        args.Object.X = oldX;
                        args.Object.Y = oldY;
                    }
                }
            }

            return base.OnCollide(args);
        }

        private IEnumerable<BoardObject> GetObjectsToMove(Direction direction)
        {
            BoardObject bo = this;
            while (bo != null)
            {
                // we don't move when the object is not movable
                if (!bo.Capabilities.HasFlag(BoardObjectCapabilities.Movable))
                {
                    break;
                }

                yield return bo;
                // do the same for the next element in the direction
                bo = bo.GetNeighbor(direction);
            }
        }

        #endregion
    }
}
